<template>
    <div>
        <h1>添加科室</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">科室名称</td>
                    <td>
                        <input type="text" v-model="info.Tname">
                    </td>
                </tr>
                <tr>
                    <td align="right">科室照片</td>
                    <td>
                        <input type="file" @change="img">
                        <img :src="info.ImgUrl" width="100px" height="100px" v-if="info.ImgUrl!=''">
                    </td>
                </tr>
                <tr>
                    <td align="right">成立日期</td>
                    <td>
                        <input type="date" v-model="info.Tdate">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-info" @click="addtype">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive}from 'vue'
import axios from 'axios'
import { useRoute,useRouter } from 'vue-router';
import { isTemplateExpression } from 'typescript';
const route = useRoute();
const router = useRouter();
let info:any=reactive({
    "Tname": "",
    "ImgUrl": "",
    "Tdate": ""
})

const addtype=()=>{
    axios({
        url:"/api/Use/AddType",
        method:"post",
        data:info
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0)
    {
        alert("添加成功");
        return;
    }
    else
    {
        alert("添加失败");
        return;
    }
    })
    .catch((err)=>{
        console.log(err);
    })
}

const img=(e:any)=>{
    let obj = e.target.files[0];
    let f = new FormData();
    f.append("file",obj);
    axios({
        url:"/api/Img/Imgs",
        method:"post",
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="大于2M")
    {
        alert("您上传图片大于2M");
        return;
    }
    else
    {
        alert("上传成功");
        info.ImgUrl = res.data;
        return;
    }
    })
    .catch((err)=>{
        console.log(err);
    })
}
</script>

<style scoped>

</style>